package com.test.deencredit.activity_deencredit;

import static com.test.deencredit.config_deencredit.PermissionConfig_deencredit.PermissionCode_PhoneState_deencredit;
import static com.test.deencredit.config_deencredit.PermissionConfig_deencredit.PermissionCode_Sms_deencredit;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.test.deencredit.R;
import com.test.deencredit.adapter_deencredit.PermissionViewAdapter_deencredit;
import com.test.deencredit.base_deencredit.BaseActivity_deencredit;
import com.test.deencredit.bean_deencredit.PermissionInfo_deencredit;
import com.test.deencredit.dialog_deencredit.KindTipDialog_deencredit;
import com.test.deencredit.listener_deencredit.PermissionListener_deencredit;
import com.test.deencredit.status_deencredit.PermissionStatus_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

import java.util.ArrayList;
import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;


public class PermissionActivity_deencredit extends BaseActivity_deencredit implements EasyPermissions.PermissionCallbacks, EasyPermissions.RationaleCallbacks {

    private ViewPager2 permissionVp2_deencredit;
    private TextView permissionTitleTv_deencredit;
    private View btnView_deencredit, agreeView_deencredit, denyView_deencredit;
    private Button denyBtn_deencredit, agreeBtn_deencredit;

    private PermissionViewAdapter_deencredit permissionViewAdapter_deencredit;
    private final ArrayList<PermissionInfo_deencredit> infos_deencredit = new ArrayList<>();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission_deencredit);
        page_deencredit = "PERMISSION";

        findViewById(R.id.aps_ib_back).setOnClickListener(v -> onBackPressed());

        permissionVp2_deencredit = findViewById(R.id.aps_vp2_permission);
        permissionTitleTv_deencredit = findViewById(R.id.aps_tv_permission_title);
        btnView_deencredit = findViewById(R.id.aps_view_btn);
        agreeView_deencredit = findViewById(R.id.aps_view_agree);
        denyView_deencredit = findViewById(R.id.aps_view_deny);
        denyBtn_deencredit = findViewById(R.id.aps_btn_deny);
        agreeBtn_deencredit = findViewById(R.id.aps_btn_agree);

        init_deencredit();
    }

    private void init_deencredit(){


        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)){

            infos_deencredit.add(new PermissionInfo_deencredit(
                    R.drawable.img_permission_sms_deencredit,
                    getString(R.string.permission_sms_title_deencredit),
                    getString(R.string.permission_sms_text_deencredit),
                    smsPermissionListneer,
                    "SMS")
            );

            infos_deencredit.add(new PermissionInfo_deencredit(
                    R.drawable.img_permission_applist_deencredit,
                    getString(R.string.permission_applist_title_deencredit),
                    getString(R.string.permission_applist_text_deencredit),
                    applistPermissionListneer,
                    "APPS")
            );

        }

        if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this,Manifest.permission.READ_PHONE_STATE)){
            infos_deencredit.add(new PermissionInfo_deencredit(
                    R.drawable.img_permission_phone_state_deencredit,
                    getString(R.string.permission_phonestate_title_deencredit),
                    getString(R.string.permission_phonestate_text_deencredit),
                    phonestatePermissionListneer,
                    "PHONE_STATE")
            );
        }

        if (!infos_deencredit.isEmpty()) permissionTitleTv_deencredit.setText(infos_deencredit.get(0).getTitle_deencredit());

        permissionViewAdapter_deencredit = new PermissionViewAdapter_deencredit(infos_deencredit);
        permissionVp2_deencredit.setAdapter(permissionViewAdapter_deencredit);

        //页面切换监听
        permissionVp2_deencredit.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);

                if (position<0 || position>= infos_deencredit.size()) return;

                PermissionInfo_deencredit info = infos_deencredit.get(position);

                permissionTitleTv_deencredit.setText(info.getTitle_deencredit());

                switch (info.getStatus_deencredit()){
                    case Agree_deencredit:
                        agreeView_deencredit.setVisibility(View.VISIBLE);
                        denyView_deencredit.setVisibility(View.GONE);
                        btnView_deencredit.setVisibility(View.GONE);
                        break;
                    case Deny_deencredit:
                        agreeView_deencredit.setVisibility(View.GONE);
                        denyView_deencredit.setVisibility(View.VISIBLE);
                        btnView_deencredit.setVisibility(View.GONE);
                        break;
                    case Default_deencredit:
                    default:
                        agreeView_deencredit.setVisibility(View.GONE);
                        denyView_deencredit.setVisibility(View.GONE);
                        btnView_deencredit.setVisibility(View.VISIBLE);

                        denyBtn_deencredit.setOnClickListener(v -> {
                            agreeView_deencredit.setVisibility(View.GONE);
                            denyView_deencredit.setVisibility(View.VISIBLE);
                            btnView_deencredit.setVisibility(View.GONE);
                            info.setStatus_deencredit(PermissionStatus_deencredit.Deny_deencredit);
                            info.onPermissionDeny_deencredit();
                        });

                        //权限同意
                        agreeBtn_deencredit.setOnClickListener(v -> info.onPermissionAgree_deencredit());

                        break;
                }

            }
        });

    }

    private final PermissionListener_deencredit smsPermissionListneer = new PermissionListener_deencredit() {
        @Override
        public void onPermissionAgree_deencredit() {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"SMS_AGREE_CLICK","");
            new KindTipDialog_deencredit(PermissionActivity_deencredit.this,
                    () -> HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"SMS_CHECKBOX_DIALOG_NO_CLICK",""),
                    () -> {
                        HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"SMS_CHECKBOX_DIALOG_YES_CLICK","");
                        EasyPermissions.requestPermissions(PermissionActivity_deencredit.this, getString(R.string.launch_permissions_sms_deencredit), PermissionCode_Sms_deencredit, Manifest.permission.READ_SMS);
                    }
            ).show();
        }

        @Override
        public void onPermissionDeny_deencredit() {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"SMS_DENY_CLICK","");
            next_deencredit();
        }

        @Override
        public void onNextClick_deencredit() {
            next_deencredit();
        }
    };

    private final PermissionListener_deencredit applistPermissionListneer = new PermissionListener_deencredit() {
        @Override
        public void onPermissionAgree_deencredit() {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"APPS_AGREE_CLICK","");
            new KindTipDialog_deencredit(PermissionActivity_deencredit.this,
                    () -> HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"APPS_CHECKBOX_DIALOG_NO_CLICK",""),
                    () -> {
                        HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"APPS_CHECKBOX_DIALOG_YES_CLICK","");
                        int p = permissionVp2_deencredit.getCurrentItem();
                        if (p>=0&&p< infos_deencredit.size()) infos_deencredit.get(p).setStatus_deencredit(PermissionStatus_deencredit.Agree_deencredit);
                        next_deencredit();
                    }
            ).show();
        }

        @Override
        public void onPermissionDeny_deencredit() {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"APPS_DENY_CLICK","");
            next_deencredit();
        }

        @Override
        public void onNextClick_deencredit() {
            next_deencredit();
        }
    };

    private final PermissionListener_deencredit phonestatePermissionListneer = new PermissionListener_deencredit() {
        @Override
        public void onPermissionAgree_deencredit() {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"PHONE_STATE_AGREE_CLICK","");
            new KindTipDialog_deencredit(PermissionActivity_deencredit.this,
                    () -> HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"PHONE_STATE_CHECKBOX_DIALOG_NO_CLICK",""),
                    () -> {
                        HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"PHONE_STATE_CHECKBOX_DIALOG_YES_CLICK","");EasyPermissions.requestPermissions(PermissionActivity_deencredit.this, getString(R.string.launch_permissions_phonestate_deencredit), PermissionCode_PhoneState_deencredit, Manifest.permission.READ_PHONE_STATE);
                    }
            ).show();
        }

        @Override
        public void onPermissionDeny_deencredit() {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"PHONE_STATE_DENY_CLICK","");
            out_deencredit();
        }

        @Override
        public void onNextClick_deencredit() {
            next_deencredit();
        }
    };

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        switch (requestCode){
            case PermissionCode_PhoneState_deencredit:

                HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"PHONE_STATE_2_DENY_CLICK","");
                if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
                    new AppSettingsDialog.Builder(this).build().show();
                }else out_deencredit();
                break;
            case PermissionCode_Sms_deencredit:

                HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"SMS_2_DENY_CLICK","");
                if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
                    new AppSettingsDialog.Builder(this).build().show();
                }else next_deencredit();

                break;
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        switch (requestCode){
            case PermissionCode_PhoneState_deencredit:
                HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"PHONE_STATE_2_AGREE_CLICK","");

                int p1 = permissionVp2_deencredit.getCurrentItem();
                if (p1>=0&&p1< infos_deencredit.size()) infos_deencredit.get(p1).setStatus_deencredit(PermissionStatus_deencredit.Agree_deencredit);

                next_deencredit();
                break;
            case PermissionCode_Sms_deencredit:
                HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"SMS_2_AGREE_CLICK","");

                int p2 = permissionVp2_deencredit.getCurrentItem();
                if (p2>=0&&p2< infos_deencredit.size()) infos_deencredit.get(p2).setStatus_deencredit(PermissionStatus_deencredit.Agree_deencredit);

                next_deencredit();
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onRationaleAccepted(int requestCode) {
        switch (requestCode){
            case PermissionCode_PhoneState_deencredit:
                EasyPermissions.requestPermissions(this, this.getString(R.string.launch_permissions_phonestate_deencredit), PermissionCode_PhoneState_deencredit, Manifest.permission.READ_PHONE_STATE);
                break;
            case PermissionCode_Sms_deencredit:
                EasyPermissions.requestPermissions(this, this.getString(R.string.launch_permissions_sms_deencredit), PermissionCode_Sms_deencredit, Manifest.permission.READ_SMS);
                break;
        }
    }

    @Override
    public void onRationaleDenied(int requestCode) {}

    public void next_deencredit(){
        int nextPageIndex = permissionVp2_deencredit.getCurrentItem()+1;
        if (nextPageIndex>=0&& permissionViewAdapter_deencredit.getItemCount()>nextPageIndex) permissionVp2_deencredit.setCurrentItem(nextPageIndex);
        else finishPermissionProcess_deencredit();
    }

    private void finishPermissionProcess_deencredit(){
        setResult(RESULT_OK);
        finish();
    }

    private void out_deencredit(){
        setResult(RESULT_OUT_APP_deencredit);
        finish();
    }


}
